package com.sinven.controller.manage;

import com.sinven.common.ActionResult;
import com.sinven.pojo.User;
import com.sinven.service.UserService;
import com.sinven.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@Slf4j
@RequestMapping("manage")
public class UserController {
    @Autowired
    private UserService userService;
    @PostMapping("users")
    public ActionResult login(String userCode, String userPwd,
                              HttpSession session ) {
        log.debug("username:{}", userCode);
        log.debug("password:{}", userPwd);

        try {
            User user = userService.login(userCode,userPwd);
            session.setAttribute("USER_SESSION",user);
            return ResultUtil.success(user);
        } catch (Exception e) {
            log.error(e.getMessage());
            return ResultUtil.error(500,e.getMessage());
        }
    }

    @GetMapping("users/logout")
    public ActionResult logOut(HttpSession session ){
        session.removeAttribute("USER_SESSION");
        return ResultUtil.success();
    }
}
